//
//  ChooseEngineerViewController.m
//  MerieuxWskProj
//
//  Created by 焱 孙 on 16/1/9.
//  Copyright © 2016年 visionet. All rights reserved.
//

#import "ChooseEngineerViewController.h"
#import "ChooseEngineerCell.h"
#import "UserVo.h"
#import "MJRefresh.h"
#import "ExtScope.h"

@interface ChooseEngineerViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    NSMutableArray *aryEngineer;
    NSMutableArray *aryFilter;
    NSInteger nPageNum;
}

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableViewEngineer;
@property (weak, nonatomic) IBOutlet UISearchDisplayController *searchDisplayController;

@end

@implementation ChooseEngineerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView
{
    [self setTopNavBarTitle:@"选择工程师"];
    
    [_tableViewEngineer registerNib:[UINib nibWithNibName:@"ChooseEngineerCell" bundle:nil] forCellReuseIdentifier:@"ChooseEngineerCell"];
    [_searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"ChooseEngineerCell" bundle:nil] forCellReuseIdentifier:@"ChooseEngineerCell"];
    
    //上拉加载更多
    @weakify(self)
    _tableViewEngineer.mj_footer =  [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self loadNewData:NO];
    }];
}

- (void)initData
{
    aryEngineer = [NSMutableArray array];
    aryFilter = [NSMutableArray array];
    
    nPageNum = 1;
    [self loadNewData:YES];
}

//分页数据
-(void)loadNewData:(BOOL)bRefresh
{
    if (bRefresh)
    {
        //下拉刷新
        nPageNum = 1;
    }
    else
    {
        //上拉加载
    }
    
    [ServerProvider getEngineerList:@"" page:nPageNum size:15 result:^(ServerReturnInfo *retInfo) {
        if (retInfo.bSuccess)
        {
            NSMutableArray *aryData = retInfo.data;
            if (bRefresh)
            {
                //下拉刷新
                if (aryData != nil && aryData.count>0)
                {
                    [aryEngineer removeAllObjects];
                    [aryEngineer addObjectsFromArray:aryData];
                }
            }
            else
            {
                //上拉加载
                [aryEngineer addObjectsFromArray:aryData];
            }
            
            if (aryData != nil && aryData.count>0)
            {
                nPageNum ++;
            }
            [_tableViewEngineer reloadData];
        }
        else
        {
            [Common tipAlert:retInfo.strErrorMsg];
        }
        
        //停止刷新
        if (bRefresh)
        {
            if([retInfo.data2 boolValue])
            {
                [_tableViewEngineer.mj_footer endRefreshingWithNoMoreData];   //最后一页
            }
            [_tableViewEngineer.mj_header endRefreshing];
        }
        else
        {
            if([retInfo.data2 boolValue])
            {
                [_tableViewEngineer.mj_footer endRefreshingWithNoMoreData];   //最后一页
            }
            else
            {
                [_tableViewEngineer.mj_footer endRefreshing];
            }
        }
    }];
}

#pragma mark Table View
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableViewEngineer)
    {
        return aryEngineer.count;
    }
    else
    {
        return aryFilter.count;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableViewEngineer)
    {
        ChooseEngineerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseEngineerCell" forIndexPath:indexPath];
        cell.entity = aryEngineer[indexPath.row];
        return  cell;
    }
    else
    {
        ChooseEngineerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseEngineerCell" forIndexPath:indexPath];
        cell.entity = aryFilter[indexPath.row];
        return  cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //采用自动计算高度，并且带有缓存机制
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UserVo *userVo;
    if(tableView == self.tableViewEngineer)
    {
        userVo = aryEngineer[indexPath.row];
    }
    else
    {
        userVo = aryFilter[indexPath.row];
    }
    
    [self.delegate completedChooseEngineer:userVo];
    
    [super backButtonClicked];
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self doSearch:searchBar.text];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //self.searchBar.frame = CGRectMake(0, 0, kScreenWidth-80, 44);
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [aryFilter removeAllObjects];
    [self.searchDisplayController.searchResultsTableView reloadData];
}

- (void)doSearch:(NSString *)strText
{
    //1.Condition Check
    if (strText == nil || strText.length<=0)
    {
        return;
    }
    
    [self isHideActivity:NO];
    [ServerProvider getEngineerList:strText page:1 size:50 result:^(ServerReturnInfo *retInfo) {
        [self isHideActivity:YES];
        if (retInfo.bSuccess)
        {
            [aryFilter removeAllObjects];
            
            NSMutableArray *aryTemp = (NSMutableArray *)retInfo.data;
            if (aryTemp != nil && [aryTemp count] > 0)
            {
                [aryFilter addObjectsFromArray:aryTemp];
                [self.searchDisplayController.searchResultsTableView reloadData];
            }
        }
    }];
}

@end
